package com.phone.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.phone.entity.Admin;
import com.phone.entity.Product;
import com.phone.entity.Store;
import com.phone.entity.User;
import com.phone.service.AdminService;
import com.phone.service.ProductService;
import com.phone.service.UserService;

@Controller
public class UserController {
	@Resource
	private UserService userServiceImpl;
	@Resource
	private ProductService productServiceImpl;
	@RequestMapping("saveuser")
	public String seleuser(HttpSession session,HttpServletRequest request,User user){
		String username = request.getParameter("username");
		User u = userServiceImpl.seleu(username);
		Admin admin=userServiceImpl.selea(username);
		Store store=userServiceImpl.seles(username);
		if (u!=null || admin!=null || store!=null) {
			session.setAttribute("message", "用户名已存在!");
			return "/registeruser.jsp";
		}else {
			userServiceImpl.saveuser(user);
			session.setAttribute("message", "注册成功!");
			return "/signin.jsp";
		}
	}
	@RequestMapping("savestore")
	public String selestore(HttpSession session,HttpServletRequest request,Store store){
		String username=request.getParameter("username");
		User user = userServiceImpl.seleu(username);
		Admin admin=userServiceImpl.selea(username);
		Store s=userServiceImpl.seles(username);
		if (user!=null || admin!=null || s!=null) {
			session.setAttribute("message", "用户名已存在!");
			return "/registerstore.jsp";	
		}else {
			userServiceImpl.savestore(store);
			session.setAttribute("message", "注册成功!");
			return "/signin.jsp";
		}
	}
	@RequestMapping("login")
	public String login(Model model,HttpSession session,HttpServletRequest request){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user=userServiceImpl.seleuser(username, password);
		Admin admin = userServiceImpl.seleadmin(username, password);
		Store store = userServiceImpl.selestore(username, password);
		List<Product> products = productServiceImpl.allproduct();
		if (user!=null) {
			session.setAttribute("user", user);
			model.addAttribute("products", products);
			return "/usershop.jsp";
		}
		else if (admin!=null) {
			session.setAttribute("admin", admin);
			model.addAttribute("products", products);
			return "/adminshop.jsp";
			}
		else if (store!=null) {
			session.setAttribute("store", store);
			model.addAttribute("products", products);
			return "/storeshop.jsp";
		}
		else {
			session.setAttribute("message", "账号或密码错误!");
			return "/signin.jsp";
		}
	}
	@RequestMapping("sele")
	public String sele(HttpServletRequest request,Model model){
		String username = request.getParameter("username");
		User user = userServiceImpl.seleu(username);
		Admin admin=userServiceImpl.selea(username);
		Store store=userServiceImpl.seles(username);
		if (user!=null) {
			model.addAttribute("user", user);
			return "/upuser.jsp";
		}else if (admin!=null) {
			model.addAttribute("admin", admin);
			return "/upadmin.jsp";
		}else {
			model.addAttribute("store", store);
			return "/upstore.jsp";
		}
	}
	@RequestMapping(value="upuser",method=RequestMethod.POST)
	public String upuser(HttpSession session,HttpServletRequest request,User user){
			userServiceImpl.upuser(user);
			session.setAttribute("message", "修改成功!");
			return "allproduct";
	}
}
